package edu.nf.supermarket.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.nf.supermarket.entity.Commodity;
import edu.nf.supermarket.entity.Orders;
import edu.nf.supermarket.mapper.CommodityMapper;
import edu.nf.supermarket.mapper.OrderMapper;
import edu.nf.supermarket.service.OrderService;
import edu.nf.supermarket.service.ProductSpecService;
import edu.nf.supermarket.util.OrderNOGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author yjj
 * @date 2025-05-21
 */
@Service("orderService")
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private CommodityMapper commodityService;
    @Autowired
    private ProductSpecService productSpecService;

    @Override
    public void addOrder(Integer userId, Orders orders, Integer commodityId, Integer commodityNum) {
        orders.setOrderId(OrderNOGenerator.generateOrderNO());
        orders.setCreateTime(LocalDateTime.now());
        orderMapper.addOrder(orders, userId);
        if (commodityId != null || commodityNum != null) {
            orderMapper.addOrderItem(orders.getOrderId(), commodityId, commodityNum,null,null);
        }
    }

    @Override
    public List<Orders> findAllOrder() {
        return orderMapper.findAllOrder();
    }

    @Override
    public PageInfo<Orders> findOrderByUserId(Integer userId, Byte status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Orders> orders = orderMapper.findOrderByUserId(userId, status);
        for(Orders order : orders){
            for (Commodity commodity : order.getCommodityList()) {
                commodity.setCommodityPicture(commodityService.findCommodityById(commodity.getCommodityId())
                        .getCommodityPicture());
            }
        }
        return new PageInfo<>(orders);
    }


    @Override
    public void updateOrderStatus(String orderId, Byte status) {
        orderMapper.updateOrderStatus(orderId, status);
    }

    @Override
    public Orders findOrderByOrderID(String orderId) {

       return orderMapper.findOrderByOrderID(orderId);


    }

    @Override
    public Orders findCommodityByUserId(Integer userId) {
        return orderMapper.findCommodityByUserId(userId);
    }

    @Override
    public void addCommodityToCart(String orderId, Integer commodityId, Integer commodityNum, String spec1, String spec2) {
        orderMapper.addCommodityToCart(orderId, commodityId, commodityNum,spec1, spec2);
    }

    @Override
    public void updateCommodityNum(String orderId, Integer commodityId, Integer commodityNum) {
        orderMapper.updateCommodityNum(orderId, commodityId, commodityNum);
    }

    @Override
    public void deleteCommodityFromCart(String orderId, Integer commodityId) {
        orderMapper.deleteCommodityFromCart(orderId, commodityId);
    }

    @Override
    public void generateOrder(Integer userId, Orders orders, Map<Integer, Commodity> commodity) {
        Orders commodityByUserId = orderMapper.findCommodityByUserId(userId);
        orderMapper.addOrder1(orders, userId);
        Set<Integer> integers = commodity.keySet();
        for (Integer commodityId : integers) {
            orderMapper.addOrderItem(orders.getOrderId(), commodityId, commodity.get(commodityId).getCommodityNum(),commodity.get(commodityId).getSpec1(), commodity.get(commodityId).getSpec2());
            if(commodityByUserId == null){
                continue;
            }
            orderMapper.deleteCommodityFromCart(commodityByUserId.getOrderId(), commodityId);
        }
    }

    @Override
    public Orders findCommodityByUserIdNoDelete(Integer orderId) {
        return orderMapper.findCommodityByUserIdNoDelete(orderId);
    }

    @Override
    public void addPayTime(LocalDateTime payTime, String orderId) {
        orderMapper.addPayTime(payTime, orderId);
    }

    @Override
    public String findOrderId(Integer userId) {
        return orderMapper.findOrderIdbyUserId(userId).getOrderId();
    }

    @Override
    public List<Orders> searchOrdersByKeyword(String keyword,Integer userId) {
        List<Orders> orders = orderMapper.searchOrdersByKeyword(keyword, userId);
        orders.forEach(order -> {
            order.getCommodityList().forEach(commodity -> {
                commodity.setCommodityPicture(commodityService.findCommodityById(commodity.getCommodityId())
                        .getCommodityPicture());
            });
        });
        return orders;
    }

    @Override
    public List<Orders> findOrdersByDateRange(LocalDate startTime, LocalDate endTime,Integer userId) {
        return orderMapper.findOrdersByDateRange(startTime, endTime, userId);
    }

    @Override
    public void deleteOrder(String orderId) {
        orderMapper.deleteOrder(orderId);
    }

    @Override
    public void cancelOrder(String orderId) {
        orderMapper.cancelOrder(orderId);
    }
}
